package io.github.wppli.trigger.http;

import cc.jq1024.middleware.token.service.ITokenService;
import io.github.wppli.domain.pay.model.entity.PayOrderEntity;
import io.github.wppli.domain.pay.model.entity.ShopCartEntity;
import io.github.wppli.domain.pay.service.IOrderService;
import io.github.wppli.types.enums.ResponseCode;
import io.github.wppli.types.model.Response;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author li--jiaqiang 2025−03−20
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/sale")
@CrossOrigin(value = {"${app.config.cross-origin:http://116.198.239.101:3000/}", "http://localhost:3000/"})
public class OrderController {

    private final ITokenService tokenService;
    private final IOrderService orderService;

    /**
     * 用户商品下单
     * 开始地址：http://localhost:8091/api/v1/sale/create_pay_order?productId=
     * 测试地址：http://apix.natapp1.cc/api/v1/sale/create_pay_order
     * <p>
     * curl -X POST \
     * -H "Authorization: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJveGZBOXc4LTI..." \
     * -H "Content-Type: application/x-www-form-urlencoded" \
     * -d "productId=1001" \
     * http://localhost:8099/api/v1/sale/create_pay_order?productId=
     */
    @RequestMapping(value = "create_pay_order", method = RequestMethod.POST)
    public Response<String> createParOrder(@RequestHeader("Authorization") String authorization, @RequestParam Long productId) {
        try {
            // todo 二维码过期处理
// 2. 获取用户信息
            String token = authorization.replace("Bearer ", "");
            String userId = tokenService.decode(token).getSubject();
            log.info("用户商品下单，根据商品ID创建支付单开始 userId:{}, productId:{}", userId, productId);
            // 生成购物车对象
            ShopCartEntity shopCartEntity = ShopCartEntity.builder()
                    .userId(Long.parseLong(userId))
                    .productId(productId)
                    .productNum(1)
                    .build();
            // 根据购物车对象创建订单对象
            PayOrderEntity payOrder = orderService.createOrder(shopCartEntity);
            if (payOrder == null) {
                return Response.<String>builder()
                        .code(ResponseCode.UN_ERROR.getCode())
                        .info(ResponseCode.UN_ERROR.getInfo())
                        .build();
            }
            log.info("用户商品下单，根据商品ID创建支付单完成 userId: {} productId: {} orderPay: {}", userId, productId, payOrder.toString());

            return Response.<String>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(payOrder.getPayUrl())
                    .build();
        } catch (Exception e) {
            log.error("用户商品下单，根据商品ID创建支付单失败", e);
            return Response.<String>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @RequestMapping(value = "create_view_order", method = RequestMethod.POST)
    public Response<String> updatePayOrder(@RequestHeader("Authorization") String authorization, @RequestParam Long productId) {
        try {
            String token = authorization.replace("Bearer ", "");
            String userId = tokenService.decode(token).getSubject();
            log.info("用户商品浏览，根据商品ID创建浏览记录开始 userId:{}, productId:{}", userId, productId);
            orderService.createViewOrder(userId, productId);
            return Response.<String>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } catch (Exception e) {
            log.error("用户商品下单，根据商品ID创建支付单失败", e);
            return Response.<String>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    // todo 获取商品列表 - 获取用户得购物车列表 - 加入购物车 - 删除购物车商品 - 获取订单列表 - 获取订单详情

}